//
//  ARViewController.m
//  OXFAM_iOS
//
//  Created by Karl on 2017/3/24.
//  Copyright © 2017年 i-Craftsmen ltd. All rights reserved.
//

#import "ARViewController.h"
#import "ManuView.h"
#import "CapturePreviewView.h"
#import "ToturialView.h"
#import "UIView+PopAnimation.h"

@interface ARViewController ()<ManuViewDelegate,NativeViewManagerDelegate>{
    UIButton *_backBtn;
    UIButton *_captureBtn;
    ManuView *_manuView;
    BOOL _toolShow;
}

@property (nonatomic,strong) CapturePreviewView *preview;
@property (nonatomic,strong) ToturialView *toturialView;

@end

@implementation ARViewController

- (void)dealloc{
    NSLog(@"%@ dealloc",NSStringFromClass([self class]));
    [[NativeViewManager sharedManager] removeDelegate:self];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.multipleTouchEnabled = YES;
    _toolShow = NO;
    [self initUI];
    [[NativeViewManager sharedManager] addDelegate:self];
}

- (void)initUI{
    _backBtn = [UIButton new];
    [_backBtn setBackgroundImage:[UIImage imageNamed:@"ar_back"] forState:UIControlStateNormal];
    [_backBtn addTarget:self action:@selector(onBackPressed:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:_backBtn];
    
    _captureBtn = [UIButton new];
    [_captureBtn setBackgroundImage:[UIImage imageNamed:@"ar_capture"] forState:UIControlStateNormal];
    [_captureBtn addTarget:self action:@selector(onCapturePressed:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:_captureBtn];
    
    _manuView = [[ManuView alloc] init];
    _manuView.delegate = self;
    [self.view addSubview:_manuView];
    
    _preview = [[CapturePreviewView alloc] init];
    [self.view addSubview:_preview];
    self.preview.hidden = YES;
}
- (void)viewDidLayoutSubviews{
    [super viewDidLayoutSubviews];
    
    CGFloat scale = 1.0;
    if (isPad) {
        scale = SizeScaleX;
    }else{
        scale = SizeScaleY;
    }
    
    _backBtn.width = 75 *scale;
    _backBtn.height = 75*scale;
    _backBtn.left = 6*scale;
    _backBtn.bottom = self.view.bottom - 30.7*scale;
    
    _captureBtn.width = 111.7*scale;
    _captureBtn.height = 111.7*scale;
    _captureBtn.right = self.view.right - 16.0*scale;
    _captureBtn.bottom = self.view.bottom - 12.3*scale;

    _manuView.width = 75*scale;
    _manuView.height = 75*scale + 50 * 4*scale;
    _manuView.top = 12*scale;
    _manuView.right = self.view.right - 34.3*scale;
    
    _preview.width = self.view.width;
    _preview.height = self.view.height;
    _preview.top = 0;
    _preview.left = 0;
    
    _toturialView.left = 0;
    _toturialView.top = 0;
    _toturialView.width = self.view.width;
    _toturialView.height = self.view.height;
}
- (ToturialView *)toturialView{
    if (!_toturialView) {
        _toturialView = [[ToturialView alloc] init];
        [self.view addSubview:_toturialView];
    }
    return _toturialView;
}

#pragma mark -

- (void)onBackPressed:(id)sender{
    NSLog(@"%@ back pressed",NSStringFromClass([self class]));

    UnitySendMessage("_sceneManager", "RouteScene", "homepage");
    [[NativeViewManager sharedManager] view:ViewTypeHomepage show:YES];
    
    [self willMoveToParentViewController:nil];
    [self.view removeFromSuperview];
    [self removeFromParentViewController];
}

- (void)onCapturePressed:(id)sender{
    NSLog(@"%@ capture pressed",NSStringFromClass([self class]));
//    [self.preview show];

    UnitySendMessage("_sceneManager", "OnClickCapture", "");
    [HUDManager showInView:nil];
    
}
- (void)manuViewAction:(NSString *)action{
    NSLog(@"manu action %@ need to process",action);
    if ([action isEqualToString:Babel(@"manu_tutorial")]) {
//        self.toturialView.hidden = NO;
        [self.toturialView pop];
    }
}
#pragma mark - NativeViewManagerDelegate

- (void)captureCompleted{
    NSLog(@"captureCompleted");
    [self.view bringSubviewToFront:self.preview];
    [HUDManager hide];
    [self.preview show];
}


#pragma mark -

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [super touchesBegan:touches withEvent:event];
    [UnityGetGLView() touchesBegan:touches withEvent:event];
}

- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [super touchesEnded:touches withEvent:event];
    [UnityGetGLView() touchesEnded:touches withEvent:event];
}

- (void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [super touchesMoved:touches withEvent:event];
    [UnityGetGLView() touchesMoved:touches withEvent:event];
}

- (void)touchesCancelled:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [super touchesCancelled:touches withEvent:event];
    [UnityGetGLView() touchesCancelled:touches withEvent:event];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
